package com.gexiaobao.pigeon.ui.fragment.mall

import android.annotation.SuppressLint
import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import androidx.appcompat.widget.AppCompatImageView
import androidx.recyclerview.widget.LinearLayoutManager
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseFragment
import com.gexiaobao.pigeon.app.ext.init
import com.gexiaobao.pigeon.app.ext.initClose
import com.gexiaobao.pigeon.app.util.StatusBarUtil
import com.gexiaobao.pigeon.app.util.Util
import com.gexiaobao.pigeon.app.weight.recycleview.SpaceItemDecoration
import com.gexiaobao.pigeon.databinding.FragmentReturnDetailBinding
import com.gexiaobao.pigeon.ui.adapter.ReturnProgressAdapter
import com.gexiaobao.pigeon.ui.mall.MallViewModel
import me.hgj.jetpackmvvm.ext.nav
import me.hgj.jetpackmvvm.ext.navigateAction
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat

/**
 * @Author      : hxw
 * @Date        : 2023/7/6 16:26
 * @Describe    : 退货详情
 */
class FragmentReturnDetail : BaseFragment<MallViewModel, FragmentReturnDetailBinding>() {

    private var orderId = 0
    private var shopId = 0
    private var goodsId = ""
    private var isReturnFinish = false
    private val mAdapter: ReturnProgressAdapter by lazy { ReturnProgressAdapter(arrayListOf()) }

    private lateinit var ivStatus: AppCompatImageView

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        activity?.let { StatusBarUtil.setLightMode(it) }
        activity?.let { StatusBarUtil.setColor(it, Color.TRANSPARENT, 0) }
    }

    @SuppressLint("MissingInflatedId")
    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.includeBar.tvWhiteTitle.text = "退货详情"
//        mDatabind.includeBar.toolbar.initClose("退货详情") {
//            nav().navigateUp()
//        }
        orderId = requireArguments().getInt("orderId")
        shopId = requireArguments().getInt("shopId")
        goodsId = requireArguments().getString("goodsId").toString()

        mDatabind.rvReturnDetail.init(LinearLayoutManager(context), mAdapter).addItemDecoration(SpaceItemDecoration(0, Util.dp2px(context, 0f)))
    }

    override fun onResume() {
        super.onResume()
        onRefresh()
    }

    private fun onRefresh() {
        showLoading(getString(R.string.loading))
        mViewModel.getReviewStatus(orderId)
    }

    @SuppressLint("NotifyDataSetChanged", "SetTextI18n")
    override fun createObserver() {
        super.createObserver()
        mViewModel.reViewsResult.observe(viewLifecycleOwner) {
            if (!it.isEmpty()) {
                val footer = LayoutInflater.from(context).inflate(R.layout.view_logic_progress_header, null)
                ivStatus = footer.findViewById(R.id.ivStatus)
                mAdapter.addFooterView(footer)

                mAdapter.setList(it.list)
                mAdapter.notifyDataSetChanged()
                isReturnFinish = it.isReturn
                if (isReturnFinish) {
                    ivStatus.visibility = View.GONE
                } else {
                    ivStatus.visibility = View.VISIBLE
                }
                it.list.forEachIndexed { _, listData ->
                    when (listData.status) {
                        3 -> {
                            mDatabind.ivStep3.setImageResource(R.mipmap.icon_logic_progress_unpass)
                            mDatabind.tvReviewRejectionReason.text = "审核驳回:" + listData.reason
                            mDatabind.llRejection.visibility = View.VISIBLE
                        }

                        1 -> {
                            mDatabind.llRejection.visibility = View.GONE
                            mDatabind.ivStep3.setImageResource(R.mipmap.icon_logic_progress_check)
                        }

                        else -> {
                            mDatabind.llRejection.visibility = View.GONE
                            mDatabind.ivStep3.setImageResource(R.mipmap.icon_logic_progress_uncheck)
                        }
                    }
                }
            }
        }
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(mDatabind.tvResubmit, mDatabind.includeBar.ivBack) {
            when (it) {
                mDatabind.includeBar.ivBack -> {
                    nav().navigateUp()
                }

                mDatabind.tvResubmit -> {
                    nav().navigateAction(R.id.action_to_fragment_return_of_goods, Bundle().apply {
                        putInt("orderId", orderId)
                        putInt("shopUserId", shopId)
                        putString("goodsId", goodsId)
                    })
                }
            }
        }
    }
}